home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 285_02 / files.c < prev    next >
Text File  |  1990-07-08  |  8KB  |  277 lines

  1. /* Open and close files for bison,
  2.    Copyright (C) 1984, 1986 Bob Corbett and Free Software Foundation, Inc.
  3.  
  4. BISON is distributed in the hope that it will be useful, but WITHOUT ANY
  5. WARRANTY.  No author or distributor accepts responsibility to anyone
  6. for the consequences of using it or for whether it serves any
  7. particular purpose or works at all, unless he says so in writing.
  8. Refer to the BISON General Public License for full details.
  9.  
  10. Everyone is granted permission to copy, modify and redistribute BISON,
  11. but only under the conditions described in the BISON General Public
  12. License.  A copy of this license is supposed to have been given to you
  13. along with BISON so you can know your rights and responsibilities.  It
  14. should be in a file named COPYING.  Among other things, the copyright
  15. notice and this notice must be preserved on all copies.
  16.  
  17.  In other words, you are welcome to use, share and improve this program.
  18.  You are forbidden to forbid anyone else to use, share and improve
  19.  what you give them.   Help stamp out software-hoarding!  */
  20.  
  21. /*
  22.     DrH. 1/4/89
  23.         Post-mortem hacking on filename handling for MS/PC-DOS conventions.
  24.         Removed obscurant PASCAL-isms in string manipulations.
  25.         Filename strings changed to static arrays to avoid stack-whacking
  26.         when constructing new names.  Why malloc strings on the fly if 
  27.         they MUST persist for the entire runtime of the program, i.e.,
  28.         are by definition static ?
  29. */
  30.  
  31.  
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35. #include <io.h>
  36.  
  37. #include "bison.h"
  38. #include "files.h"
  39. #include "new.h"
  40. #include "gram.h"
  41.  
  42. FILE *finput = NULL;
  43. FILE *foutput = NULL;
  44. FILE *fdefines = NULL;
  45. FILE *ftable = NULL;
  46. FILE *fattrs = NULL;
  47. FILE *fguard = NULL;
  48. FILE *faction = NULL;
  49. FILE *fparser = NULL;
  50.  
  51. /* File name specified with -o for the output file, or 0 if no -o.  */
  52. char *spec_outfile;
  53.  
  54. char *infile;
  55. char outfile[80] = "\0" ;
  56. char defsfile[80] = "\0" ;
  57. char tabfile[80] = "\0" ;
  58. char attrsfile[ FNAMESIZE + 1] = "\0" ;
  59. char guardfile[ FNAMESIZE + 1] = "\0" ;
  60. char *actfile;
  61. char *tmpattrsfile;
  62. char *tmptabfile;
  63.  
  64. static FILE *tryopen( char *, char *) ;
  65. static char *get_namebase( char *) ;
  66. static char *name_base;
  67.  
  68. extern int verboseflag;
  69. extern int definesflag;
  70.  
  71. int fixed_outfiles = 0;
  72.  
  73.  
  74.  
  75. /* JF this has been hacked to death.  Nowaday it sets up the file names for
  76.    the output files, and opens the tmp files and the parser */
  77.  
  78. /*--------------------------------------------------------------------------
  79. ** OPENFILES
  80. **      constructs output/temp filenames from whatever is handy.
  81. **      and attempts to open them all.
  82. */
  83. void openfiles()
  84. {
  85.   char *filename;
  86.  
  87.   if (*spec_outfile)             /* output file was specified via '-o' option */
  88.       name_base = get_namebase( spec_outfile) ;
  89.   else
  90.   {                                /* base filename is "ytab" if -y flag used */
  91.         if (fixed_outfiles)
  92.             name_base = "ytab" ;
  93.         else
  94.             name_base = get_namebase( infile) ;
  95.   }
  96.  
  97.   finput = tryopen(infile, "r") ;
  98.  
  99.   if (!(filename = getenv("BISON_SIMPLE")))      /* check if parser skeleton */
  100.      filename = PFILE ;                           /* in environment variable  */
  101.  
  102.   fparser = tryopen( filename, "r") ;
  103.  
  104.   if (verboseflag)                                /* open parser stats file */
  105.     {
  106.       sprintf( &outfile[0], "%s%s", name_base, ".out") ;
  107.       foutput = tryopen(outfile, "w") ;
  108.     }
  109.  
  110.   if (definesflag)                               /* open definitions file */
  111.     {
  112.       sprintf( &defsfile[0], "%s%s", name_base, ".h") ;
  113.       fdefines = tryopen(defsfile, "w") ;
  114.     }
  115.                                          /* open temporary files        */
  116.   actfile = mktemp("acXXXXXX") ;         /* NOTE: temps were previously */
  117.                                          /* unlinked after opening in the */
  118.   faction = tryopen(actfile, "w+") ;     /* GNU release version (ca. 9/88) */
  119.                                          
  120.   tmpattrsfile = mktemp("atXXXXXX") ;    
  121.   fattrs = tryopen(tmpattrsfile,"w+") ;  
  122.  
  123.   tmptabfile = mktemp("taXXXXXX") ;
  124.   ftable = tryopen(tmptabfile, "w+") ;
  125.  
  126.     /* These are opened by `done' or `open_extra_files', if at all */
  127.     /* N.B. tabfile is the filename of the generated parser (DrH)  */
  128.  
  129.   if (spec_outfile && *spec_outfile)
  130.     strcpy( &tabfile[0], spec_outfile) ;
  131.   else
  132.     sprintf( &tabfile[0], "%s%s", name_base, ".c") ;
  133.  
  134.   sprintf( &attrsfile[0], "%s%s", name_base, ".atr") ;
  135.   sprintf( &guardfile[0], "%s%s", name_base, ".grd") ;
  136. }
  137.  
  138.  
  139. /* open the output files needed only for the semantic parser.
  140. This is done when %semantic_parser is seen in the declarations section.  */
  141. void open_extra_files()
  142. {
  143.   FILE *ftmp;
  144.   int c;
  145.   char *filename;
  146.         /* JF change open parser file */
  147.   fclose( fparser) ;
  148.  
  149.   if (!(filename = getenv("BISON_HAIRY")))      /* check if parser skeleton */
  150.      filename = PFILE1 ;                         /* in environment variable  */
  151.  
  152.   fparser = tryopen( filename, "r") ;
  153.  
  154.         /* JF change from inline attrs file to separate one */
  155.   ftmp = tryopen( attrsfile, "w") ;
  156.   rewind( fattrs) ;
  157.  
  158.   while ((c = getc( fattrs)) != EOF)    /* Thank god for buffering */
  159.     putc(c,ftmp) ;
  160.  
  161.   fclose(fattrs) ;
  162.   fattrs=ftmp;
  163.  
  164.   fguard = tryopen(guardfile, "w") ;
  165.  
  166. }
  167.  
  168.     /* JF to make file opening easier.  This func tries to open file
  169.        NAME with mode MODE, and prints an error message if it fails. */
  170. FILE *
  171. tryopen(name, mode)
  172. char *name;
  173. char *mode;
  174. {
  175.  FILE   *ptr;
  176.  
  177.     ptr = fopen(name, mode) ;
  178.  
  179.     if (ptr == NULL)
  180.     {
  181.       fprintf(stderr, "bison file open: ") ;
  182.       perror(name) ;
  183.       done(2) ;
  184.     }
  185.  
  186.     return ptr;
  187. }
  188.  
  189. /*
  190.     DrH 1/6/89
  191.         change file copy to block mode
  192. */
  193.  
  194. void done(k)
  195. int k;
  196. {
  197.  char *buff ;
  198.  int bytesread = 0 ;
  199.  
  200.   if (faction)
  201.   {
  202.     fclose(faction) ;
  203.     unlink(actfile) ;
  204.   }
  205.  
  206.   if (fattrs)
  207.   {
  208.     fclose(fattrs) ;
  209.     unlink(tmpattrsfile) ;
  210.   }
  211.  
  212.   if (fguard)
  213.     fclose(fguard) ;
  214.  
  215.   if (finput)
  216.     fclose(finput) ;
  217.  
  218.   if (fparser)
  219.     fclose(fparser) ;
  220.  
  221.   if (foutput)
  222.     fclose(foutput) ;
  223.  
  224.     /* JF write out the output file */
  225.   if (k == 0 && ftable)
  226.     {
  227.       FILE *ftmp;
  228.       register int c;
  229.  
  230.       ftmp = tryopen( tabfile, "w") ;
  231.       rewind(ftable) ;
  232.  
  233.       buff = NEW2( 4608, char) ;
  234.  
  235.       while ((bytesread = fread( buff, 1, 4608, ftable)) > 0)
  236.         (void) fwrite( buff, 1, bytesread, ftmp) ;
  237.       FREE( buff) ;
  238.  
  239.       fclose( ftmp) ;
  240.       fclose( ftable) ;
  241.       unlink(tmptabfile) ;
  242.     }
  243.  
  244.   exit(k) ;
  245. }
  246.  
  247. /*----------------------------------------------------------------------------
  248. ** GET_NAMEBASE
  249. **      extracts base file name from an MS/PC-DOS path string.
  250. **      makes naive assumptions about initial correctness of path string.
  251. **  (DrH 1/5/88)
  252. */
  253. static char *get_namebase( char *path)
  254. {
  255.  int i ;
  256.  char *cp ;
  257.  static char filename[ FNAMESIZE + 1 ] ;
  258.  
  259.     if (!*path)                                     /* if no input string...*/
  260.         path = "ytab    .y  " ;
  261.  
  262.     for(i=0 ; i<FNAMESIZE ; i++)                    /* init string space */
  263.         filename[i] = '\0' ;
  264.  
  265.     if (cp = strrchr( path, '\\'))           /* find rightmost path separator */
  266.         ++cp ;
  267.     else
  268.         cp = path ;
  269.                                                           /* copy  up to '.' */
  270.     for(i = 0 ; ((i < 8) && *cp && (*cp != '.')) ; i++)
  271.         filename[i] = *cp++ ;
  272.     filename[i] = '\0' ;
  273.  
  274.     return ( &filename[0] ) ;
  275. }
  276.  
  277.